package com.kl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kl.domain.CarDTO;
import com.kl.entity.Car;
import com.kl.mapper.CarMapper;
import com.kl.service.ICarService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2021-10-29
 */
@Service
public class CarServiceImpl extends ServiceImpl<CarMapper, Car> implements ICarService {

    @Resource
    private CarMapper carMapper;

    // name="pageNumber",value = "起始页数"
    // name="pageSize",value = "每页条数"
    // name="carType",value = "车的类型"
    // name="site",value = "所属站点"
    // name="state",value = "当前状态"
    // name="carDriver",value = "车辆司机"
    @Override
    public Map<String,Object> selectCarLikePage(Integer pageNumber, Integer pageSize, Integer carType, String site, String state, String carDriver) {


        QueryWrapper<Car> query= new QueryWrapper<>();

            query.like(StringUtils.isNoneBlank(carDriver),"car_driver",carDriver)
            .or().eq(StringUtils.isNoneBlank(state),"state",state)
            .or().eq(StringUtils.isNoneBlank(site),"site",site)
            .or().eq(carType == null,"car_type",carType);


        //第三个参数 如为false 则不查总条数(只要数据,发送一条sql,损耗较小) 如果为true 则查总条数(发送两条sql)
        Page<Car> page = new Page<>(pageNumber,pageSize ,true);

        IPage<Car> carIPage = carMapper.selectPage(page, query);
        List<Car> records = page.getRecords();

        List<CarDTO> list = new ArrayList<>();
        for (Car car : records) {
            CarDTO carDTO = new CarDTO();
            BeanUtils.copyProperties(car,carDTO);
            list.add(carDTO);
        }

        Map<String,Object> map = new HashMap<>();
        map.put("total",carIPage.getTotal());
        map.put("result",list);
        return map;
    }
}
